export function ArrayDataString(str) {
  return str.split(" ");
}

export  function range(start, to, step = 1) {
  let nums = [];
  for (let i = start; i <= to; i += step) {
    nums.push(i);
  }
  return nums;
}

// nuumbers: {'佰': [2,3], '拾': [3,4]}
export function serializeContent(numbers) {
  let strs = [];
  for (let groupName in numbers) {
    strs.push(`${groupName}:` + numbers[groupName].join(',') );
  }
  return strs.join('|');
}

// number: 佰:2,3|拾:3,5
export function unserializeContent(number) {
  let parts = number.split('|');
  let unserializeContent = {};
  for (let part of parts) {
    let subParts = part.split(':');
    let nums = [];
    if (subParts[1] && subParts[1].trim().length > 0) {
      nums = subParts[1].split(',');
    }
    unserializeContent[subParts[0]] = nums.map( num => num * 1);
  }

  return unserializeContent;
}

export function buildQuery(query = {}) {
  let esc = encodeURIComponent;
  return Object.keys(query).map( (key) => {
    return esc(key) + '=' + esc(query[key])
  }).join('&');
}

export function parseQuery(qstr) {
  if (qstr.trim().length <= 0) {
    return {};
  }
  var query = {};
  var a = (qstr[0] === '?' ? qstr.substr(1) : qstr).split('&');
  for (var i = 0; i < a.length; i++) {
      var b = a[i].split('=');
      let v = b[1] || '';
      try {
        v = decodeURIComponent(b[1] || '');
      } catch (e) {
        // TODO::
      }
      query[decodeURIComponent(b[0])] = v;
  }
  return query;
}

export function dingweiTotal(content,num) {
  var group = {};
  var total = 1;
  var parts = content.split('|');
  if (parts.length < 2 ) return 0;
  for (var i = 0; i < parts.length; i++){
    var ps = parts[i].split(':');
    if (typeof group[ps[0]] == 'undefined') {
      group[ps[0]] = [];
    }
    group[ps[0]] = group[ps[0]].concat(ps[1].split(','));
  }
  // console.log(group)
  for (var key in group) {
    if(group[key] == "" ){
      //  console.log(group[key] )
      return 0
    }
    total *= group[key].length;
  }
  return total;
}


export  function orderNum(minlen,maxlength,cb=()=>{}){
  var temp = []; // 防止随机出来的数字是一样的
  for(let i=0; i<minlen;i++){
    var randomNum =  parseInt(Math.random()*(maxlength),10)
    tempfuc(temp,randomNum)
    function tempfuc(temp,randomNum){
      if(temp.indexOf(randomNum) > -1){
        var randomNum2 =  parseInt(Math.random()*(maxlength),10)
        tempfuc(temp,randomNum2)
      }else{
        temp.push(randomNum)
      }
    }
    cb(temp[i])
   }
   return temp
}